package cn.regexp.web.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopCart;
import cn.regexp.domain.ShopUser;
import cn.regexp.redis.service.IShopUserRedisService;
import cn.regexp.service.ICartService;
import cn.regexp.utils.JsonResult;
import cn.regexp.web.model.CartModel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("carts")
public class CartController {
    @Autowired
    private ICartService cartService;
    @Autowired
    private IShopUserRedisService shopUserRedisService;

    @PostMapping("/addGoods")
    public JsonResult<ShopCart> addGoods(@RequestBody Long goodsId, HttpServletRequest request) {
        ShopUser shopUser = shopUserRedisService.getUserByToken(request.getHeader("token"));
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        if (ObjectUtil.isEmpty(goodsId)) {
            return JsonResult.defaultError();
        }
        Boolean res = cartService.addGoods(goodsId, shopUser.getId());
        return res ? JsonResult.success() : JsonResult.defaultError();
    }

    @PostMapping("/subGoods")
    public JsonResult<ShopCart> subGoods(@RequestBody Long goodsId, HttpServletRequest request) {
        ShopUser shopUser = shopUserRedisService.getUserByToken(request.getHeader("token"));
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        if (ObjectUtil.isEmpty(goodsId)) {
            return JsonResult.defaultError();
        }
        Boolean res = cartService.subGoods(goodsId, shopUser.getId());
        return res ? JsonResult.success() : JsonResult.defaultError();
    }

    @PostMapping("/delGoods")
    public JsonResult<ShopCart> delGoods(@RequestBody Long goodsId, HttpServletRequest request) {
        ShopUser shopUser = shopUserRedisService.getUserByToken(request.getHeader("token"));
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        if (ObjectUtil.isEmpty(goodsId)) {
            return JsonResult.defaultError();
        }
        Boolean res = cartService.delGoods(goodsId, shopUser.getId());
        return res ? JsonResult.success() : JsonResult.defaultError();
    }

    @GetMapping("/queryCart")
    public JsonResult<List<CartModel>> queryCart(HttpServletRequest request) {
        ShopUser shopUser = shopUserRedisService.getUserByToken(request.getHeader("token"));
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        // 查询指定用户的购物车商品
        QueryWrapper<ShopCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", shopUser.getId())
                .eq("is_pay", ShopCart.NO);
        List<ShopCart> cartList = cartService.list(wrapper);

        List<CartModel> cartModels = new ArrayList<>();
        // 根据窗口名称进行分组
        Map<Long, List<ShopCart>> cartListGroupByWindowName = cartList.stream().collect(Collectors.groupingBy(ShopCart::getWindowId));
        cartListGroupByWindowName.keySet().forEach(item -> {
            CartModel cartModel = new CartModel();
            cartModel.setWindowId(item);
            List<ShopCart> shopCarts = cartListGroupByWindowName.get(item);
            shopCarts.sort((o1, o2) -> DateUtil.compare(o1.getUpdateTime(), o2.getUpdateTime()));
            cartModel.setWindowName(shopCarts.get(0).getWindowName());
            cartModel.setCartList(shopCarts);
            cartModel.setUpdateTime(shopCarts.get(shopCarts.size() - 1).getUpdateTime());
            cartModels.add(cartModel);
        });
        cartModels.sort((o1, o2) -> DateUtil.compare(o2.getUpdateTime(), o1.getUpdateTime()));
        return JsonResult.success(cartModels);
    }


    @PostMapping("/changeGoodsChecked")
    public JsonResult<String> changeGoodsChecked(@RequestBody Long goodsId, HttpServletRequest request) {
        ShopUser shopUser = shopUserRedisService.getUserByToken(request.getHeader("token"));
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        if (ObjectUtil.isEmpty(goodsId)) {
            return JsonResult.defaultError();
        }
        Boolean res = cartService.changeGoodsChecked(goodsId, shopUser.getId());
        return res ? JsonResult.success() : JsonResult.defaultError();
    }

    @GetMapping("/changeGoodsAllChecked")
    public JsonResult<String> changeGoodsAllChecked(Long windowId, Boolean flag, HttpServletRequest request) {
        ShopUser shopUser = shopUserRedisService.getUserByToken(request.getHeader("token"));
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        if (ObjectUtil.isEmpty(windowId)) {
            return JsonResult.defaultError();
        }
        Boolean res = cartService.changeGoodsAllChecked(windowId, flag ? 1 : 0, shopUser.getId());
        return res ? JsonResult.success() : JsonResult.defaultError();
    }

}
